import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入

@Component({
  selector: 'sa-fence',
  templateUrl: './fence.component.html'
})
export class FenceComponent implements OnInit {
  tableData: any = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  searchKey: string = '' //搜索keyword

  startTime: string = ''
  endTime: string = ''
  companyId: string = ''
  companyName: string = ''

  initDateRange: any = {
    startTime: '',
    endTime: ''
  } // 时间组件需要渲染的值

  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage || 1
    })
  }

  companySelected(data) {
    this.companyId = data.companyId || ''
    this.companyName = data.companyName
  }

  ngOnInit() {
    this.load()
  }
  // 接收子组件传递过来的数据
  getSelectTableTime(time) {
    this.startTime = time.startTime
    this.endTime = time.endTime
  }
  //table刷新
  refresh() {
    this.load()
  }

  //表格搜索
  table_search() {
    this.curPage = 1
    this.searchKey = common.stringTrim(this.searchKey)
    this.load()
  }
  //初始化加载list数据
  load() {
    if (this.startTime && !this.endTime) {
      this.jsonApiService.message('请选择结束时间')
      return
    }
    if (!this.startTime && this.endTime) {
      this.jsonApiService.message('请选择开始时间')
      return
    }
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        searchKey: this.searchKey,
        startDateStr: this.startTime,
        endDateStr: this.endTime,
        companyId: this.companyId
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('factoryFence/getFactoryFencePage', reqdata).subscribe(data => {
      this.tableData = data.exhibitDatas
      this.totalCount = data.recordCount
    })
  }

  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  //新建公司
  new_company() {
    this.router.navigate(['/data-statistics/task/fence/edit'], { queryParams: { type: 'new' } })
  }
  //编辑页面
  update_company(row) {
    this.router.navigate(['/data-statistics/task/fence/edit'], {
      queryParams: { id: row.id, type: 'update', nowPage: this.curPage }
    })
  }
  //删除
  del_company(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除?').then(() => {
      let reqdata = 'ids=' + row.id
      this.jsonApiService.http('factoryFence/deleteBatch', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  //根据公司行业类型id获取行业类型中文
  getBusinessTypeText(businessType) {
    const item = businessTypeList.find(item => {
      return item.value === businessType
    })
    return item ? item.label : '--'
  }
}
